﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CreditsSceneScript : MonoBehaviour {

	SpriteRenderer slideRender;
	SpriteRenderer barRender;
	GameObject creditHolder;

	List<TextMesh> scrollRenderers = new List<TextMesh>();
	List<TextMesh> textMeshes = new List<TextMesh>();

	SpriteRenderer backRend;
	Animator backAnim;

	float timeBetweenIncrements = 0.01f;
	float currentTimeBetweenIncrement = 0.01f;
	float incrementPosition = 0.01f;

	// Use this for initialization
	void Start () {
		this.backAnim = (Animator)GameObject.Find("Back").GetComponent(typeof(Animator));
		this.backRend = (SpriteRenderer)GameObject.Find("Back").GetComponent(typeof(SpriteRenderer));

		this.slideRender = (SpriteRenderer)GameObject.Find("Slider").GetComponent(typeof(SpriteRenderer));
		this.barRender = (SpriteRenderer)GameObject.Find("SliderBar").GetComponent(typeof(SpriteRenderer));
		
		this.creditHolder = (GameObject)GameObject.Find("CreditHolders");
		int children = this.creditHolder.transform.childCount;
		for (int z=0;z<children;z++) {
			Transform child = this.creditHolder.transform.GetChild(z);
			if (child.name != "ScrollMe" && child.name != "ScrollText") {
				TextMesh rend = (TextMesh)child.gameObject.GetComponent(typeof(TextMesh));
				//TextMesh textMesh = (TextMesh)child.GetChild(0).gameObject.GetComponent(typeof(TextMesh));
				this.scrollRenderers.Add(rend);
				//this.textMeshes.Add(textMesh);
			}
		}

		this.UpdateBarFade();
		
		this.backAnim.SetInteger(MenuButtonConstants.MenuTriggerString, (int)MenuButtonConstants.MenuConstants.BackUp);
	}

	void UpdateBarFade() {
		Vector3 minBounds = Camera.main.WorldToScreenPoint(this.barRender.bounds.min);
		Vector3 maxBounds = Camera.main.WorldToScreenPoint(this.barRender.bounds.max);
		
		
		float barMin = minBounds.y;
		float barMax = maxBounds.y;
		
		for (int z=0;z<scrollRenderers.Count;z++) {
			TextMesh rend = this.scrollRenderers[z];
			//TextMesh rendText = this.textMeshes[z];

			Vector3 rendMin = Camera.main.WorldToScreenPoint(rend.renderer.bounds.min);
			Vector3 rendMax = Camera.main.WorldToScreenPoint(rend.renderer.bounds.max);

			Color col = rend.color;
			Vector3 scaleRend = rend.transform.localScale;
			if (rendMin.y > barMax) {
				float a = Mathf.Lerp(1f,0f,(rendMin.y - barMax) / ((float)Screen.height - barMax));
				float sc = Mathf.Lerp(0.05f,0.1f,(rendMin.y - barMax) / ((float)Screen.height - barMax));
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			else if (rendMax.y < barMin) {
				float a = Mathf.Lerp(0f,1f,rendMax.y / barMin);
				float sc = Mathf.Lerp(0.0f,0.05f,rendMax.y / barMin);
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			else {
				float a = 1f;
				float sc = 0.05f;
				col.a = a;
				scaleRend.x = scaleRend.y = sc;
			}
			//Debug.Log (child.name + "/" + barMin + "/" + barMax + "/" + col.ToString());
			//rendText.color = col;
			rend.color = col;
			rend.transform.localScale = scaleRend;
		}
		
	}

	float minimumCredits = -4.7f;
	float maximumCredits = 9f;

	// Update is called once per frame
	void Update () {
		bool inScrollBar = false;
		bool inScroll = false;

		bool inBack = false;
		bool inBackInitially = false;
		
		if (GameMaintainerSingleton.Instance.CheckInTouchInteractionState()) {
			inBack = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.Pos);
			inBackInitially = GameMaintainerSingleton.Instance.IsInSpriteBounds(this.backRend, GameMaintainerSingleton.Instance.ClickDownPos);

			inScroll = GameMaintainerSingleton.Instance.IsInSpriteX(this.slideRender, GameMaintainerSingleton.Instance.ClickDownPos,10);
			inScrollBar = GameMaintainerSingleton.Instance.IsInSpriteX(this.barRender, GameMaintainerSingleton.Instance.Pos,10);
			
			GameMaintainerSingleton.SpriteBoundsSplitViewModel vm = new GameMaintainerSingleton.SpriteBoundsSplitViewModel();
			if (inScroll) {
				vm = GameMaintainerSingleton.Instance.CheckBarNoSplitVertical(
					inScroll,inScrollBar,slideRender,barRender,
					GameMaintainerSingleton.Instance.Pos, GameMaintainerSingleton.Instance.ClickDownPos, 40); 
				
				//Min 4.75
				//Max -17.41418
				Vector3 mainMenuPos = this.creditHolder.transform.position;
				mainMenuPos.y = Mathf.Lerp(maximumCredits,minimumCredits,vm.RelativeDistance);
				this.creditHolder.transform.position = mainMenuPos;
				
				this.UpdateBarFade();
			}
		}
		else {
			if (currentTimeBetweenIncrement < 0f) {
				currentTimeBetweenIncrement = timeBetweenIncrements;

				Vector3 mainMenuPos = this.creditHolder.transform.position;
				if (mainMenuPos.y < maximumCredits) {
					mainMenuPos.y += incrementPosition;
				}
				Vector3 minBounds = this.barRender.bounds.min;
				Vector3 maxBounds = this.barRender.bounds.max;

				Vector3 minSliderBounds = this.slideRender.bounds.min;
				Vector3 maxSliderBounds = this.slideRender.bounds.max;
				float sliderDistance = maxSliderBounds.y - minSliderBounds.y;

				float updatedYPosition = Mathf.Lerp(maxBounds.y,minBounds.y,(mainMenuPos.y - minimumCredits) / (maximumCredits - minimumCredits));
				Vector3 sliderPos =  this.slideRender.transform.position;
				sliderPos.y = updatedYPosition;
				this.slideRender.transform.position = sliderPos;
				this.creditHolder.transform.position = mainMenuPos;
				
				this.UpdateBarFade();
			}
			else {
				currentTimeBetweenIncrement -= Time.deltaTime;
			}
		}
		
		// Play Button
		if (GameMaintainerSingleton.Instance.CheckButton(
			inBack,inBackInitially,
			backRend,backAnim,
			MenuButtonConstants.MenuConstants.BackDown,MenuButtonConstants.MenuConstants.BackUp,
			MenuButtonConstants.MenuTriggerString)) {
			Application.LoadLevel(0);
		}

	}
}
